<?php

namespace app\order\controller;

use think\Controller;
use think\facade\Log;


/**
 * 如果你不方便配置命令行，则可以执行URL方式
 * 订单定时任务
 * @author 晓风<215628355@qq.com>
 */
class Timing extends Controller
{
   
    //自动取消订单申请钩子 15分钟一次
    //http://localhost/index.php/order/timing/auto_cancel_order
    public function auto_cancel_order()
    {      
        Log::info('------自动取消订单开始------');
        set_time_limit(0);
        //加进程锁
        $locKey = "lock_cancel_order" ;
        $redis = \app\common\model\Redis::handler();
        $lock = $redis->get($locKey);
        if ($lock){
            return false;
        }
        $redis->setnx($locKey, 1);      
        try {
            \think\facade\Hook::exec([\app\order\event\CancelOrder::class,"run"]);  
        } catch (\Exception $e) { 
            Log::error($e->getMessage());
        }finally{
            $redis->del($locKey);
        }    
        Log::info('------自动取消订单结束------');
    }
    //自动取消订单结算钩子 15分钟一次
    //http://localhost/index.php/order/timing/auto_cancel_order_refund
    public function auto_cancel_order_refund(){
        Log::info('------自动取消订单结算开始------');
        set_time_limit(0);
        //加进程锁
        $locKey = "lock_cancel_order_refund" ;
        $redis = \app\common\model\Redis::handler();
        $lock = $redis->get($locKey);
        if ($lock){
            return false;
        }
        $redis->setnx($locKey, 1);      
        try {
            \think\facade\Hook::exec([\app\order\event\CancelOrder::class,"refund"]);  
        } catch (\Exception $e) { 
            Log::error($e->getMessage());
        }finally{
            $redis->del($locKey);
        }  
        Log::info('------自动取消订单结算结束------');
        
    }
    
    //自动确认收货钩子 15分钟一次
    //http://localhost/index.php/order/timing/auto_receiving
    public function auto_receiving(){
        Log::info('------自动确认收货开始------');
        set_time_limit(0);
        //加进程锁
        $locKey = "lock_order_receiving" ;
   
        $redis = \app\common\model\Redis::handler();
        $lock = $redis->get($locKey);
        if ($lock) return false;
        try {
            \think\facade\Hook::exec([\app\order\event\ReceiveOrder::class,"run"]);  
        } catch (\Exception $e) { 
             Log::error($e->getMessage());
        }finally{
            $redis->del($locKey);
        }  
   
        Log::info('------自动确认收货结束------');        
    }    
    //自动结算钩子 15分钟一次
    //http://localhost/index.php/order/timing/settlement
    public function settlement(){
        Log::info('------执行结算开始------');
        set_time_limit(0);
        //加进程锁
        $locKey = "lock_order_settlement" ;
        $redis = \app\common\model\Redis::handler();
        $lock = $redis->get($locKey);
        if ($lock) return false;
        try {
            \think\facade\Hook::exec([\app\order\event\Settlement::class,"run"]);  
        } catch (\Exception $e) { 
            Log::error($e->getMessage());
        }finally{
            $redis->del($locKey);
        }      
        Log::info('------执行结算结束------');        
    }
}